#include <QDebug>
#include <QUrl>
#include <QMessageBox>
#include <QLabel>

#include "ftphandler.h"
#include "globals.h"

FtpHandler::FtpHandler(QObject* parent):QObject(parent)
{

}

FtpHandler::~FtpHandler()
{
	delete m_ftp;
}

void FtpHandler::connectOrDisconnect()
{
	if (m_ftp)
	{
		qDebug() << "abort and close connection";
		m_ftp->abort();
		m_ftp->deleteLater();
		m_ftp= NULL;
		delete m_file;
		return;
	}

	m_ftp = new QFtp(this);
	connect(m_ftp , SIGNAL(commandFinished(int, bool)),
			this, SLOT(ftpCommandFinished(int, bool)));	
	connect(m_ftp , SIGNAL(dataTransferProgress(qint64, qint64)),
			this, SLOT(updateDataTransferProgress(qint64, qint64)));
	connect(m_ftp, SIGNAL(done(bool)), SLOT(ftpDone(bool)));

	if( m_usePassiveMode )
		m_ftp->setTransferMode(QFtp::Passive);
	else
		m_ftp->setTransferMode(QFtp::Active);

	m_ftp->connectToHost(QUrl(m_host).host());
	m_ftp->login(m_username, m_password);
}

void FtpHandler::ftpCommandFinished(int, bool error)
{
	switch( m_ftp->currentCommand() )
	{
		case QFtp::ConnectToHost:
			qDebug() << "QFtp::ConnectToHost";
			if (error)
			{
				emit(ftpError( tr("Unable to connect to the FTP server:") + m_host ));
				qDebug() <<  tr("Unable to connect to the FTP server .");
				connectOrDisconnect();
			}
			else
				qDebug() << "loggedIn";
			break;
						
		case QFtp::Login:
			qDebug() << "QFtp::Login";
			if( !error )
			{
				qDebug() << "login ing";
				m_ftp->cd(TODOFTPDIR);
			}
			else
			{
				qDebug() << "error occured" << m_ftp->error();
				connectOrDisconnect();
			}
			break;

		case QFtp::Mkdir:
			qDebug() << "QFtp::Mkdir";
			if( !error )
				m_ftp->cd(TODOFTPDIR);
			else
				displayError();
			break;
			
		case QFtp::Cd:
			qDebug() << "QFtp::Cd:";
			if( !error )
			{
				qDebug() << "we are in the directory, get or upload file";
				if( m_download )
					downloadFile();
				else
					uploadFile();
			}
			else
			{
				displayError();
				if( m_ftp->error() == QFtp::UnknownError )
				{
					qDebug() << "Launch mkdir";
					m_ftp->mkdir(TODOFTPDIR);
					qDebug() << "launched";
				}				
			}
			break;

		case QFtp::Put:
			qDebug() << "QFtp::Put:";
			if( !error )
			{
				//QMessageBox::information(this, "QWhatToDo", tr("File uploaded successfull"));				
			}
			else
				qDebug() << tr("Uploading config file failed");
			
			connectOrDisconnect();
			break;

		case QFtp::Get:
			qDebug() << "QFtp::Get:";
			if( !error )
			{
				qDebug() << tr("File download successfull");
				//emit(ftpDone());	
			}
			else
			{
				qDebug() << tr("Downloading config file failed");
			}

			connectOrDisconnect();
			break;
			
		default:
			break;
	}
}

void FtpHandler::updateDataTransferProgress(qint64 readBytes,
								qint64 totalBytes)
{
	emit(ftpProgress( readBytes, totalBytes ));
}

void FtpHandler::displayError()
{
	qDebug() << "Error occured: " << m_ftp->error();	
}

void FtpHandler::uploadFile()
{
	qDebug() << "uploadFile";
	if( m_file )
		delete m_file;
	
	m_file = new QFile(DATAFILE);
	if( m_file->open(QIODevice::ReadOnly))
	{				
		m_ftp->put(m_file, DATAFILE);	
	}
}

void FtpHandler::downloadFile()
{	

	m_file = new QFile(DATAFILEFTP);
	if( m_file->open(QIODevice::WriteOnly | QIODevice::Truncate))
	{	
		m_ftp->get(DATAFILE, m_file);
	}
}

void FtpHandler::ftpUpload()
{
	m_download = false;
	connectOrDisconnect();
}

void FtpHandler::ftpDownload()
{
	m_download = true;
	connectOrDisconnect();
}

void FtpHandler::ftpDone(bool error)
{
	//we are done here:
	if( !error )
		emit(ftpDone());
	else
		emit(ftpError( tr("FTP error: ") + m_ftp->error()));
}

